// CSE 142, Winter 2008, Marty Stepp // // This program demonstrates a "sentinel" value, // a special value that signals the end of user input. // Sentinel loops are an example of a fencepost problem. import java.util.*; public class Sentinel { public static void main(String[] args) { Scanner console = new Scanner(System.in); // read first number (place first "post") System.out.print("Enter a number (-1 to quit): "); int number = console.nextInt(); int sum = 0; // read remaining numbers while (number != -1) { // add number to sum (place a "wire") sum = sum + number; // read next number (place a "post") System.out.print("Enter a number (-1 to quit): "); number = console.nextInt(); } System.out.println("The total was " + sum); } }